<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li class="active"><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 初始化项目">1. 初始化项目</a></li><li><a href="#t12.安装依赖">2.安装依赖</a></li><li><a href="#t23.支持typescript">3.支持typescript</a></li><li><a href="#t34.编写webpack配置文件">4.编写webpack配置文件</a></li><li><a href="#t45.计数器组件">5.计数器组件</a><ul><li><a href="#t55.1 src/index.tsx">5.1 src/index.tsx</a></li><li><a href="#t65.2 components/Counter.tsx">5.2 components/Counter.tsx</a></li></ul></li><li><a href="#t76. 使用redux">6. 使用redux</a><ul><li><a href="#t86.1 src/index.tsx">6.1 src/index.tsx</a></li><li><a href="#t96.2 components/Counter.tsx">6.2 components/Counter.tsx</a></li><li><a href="#t106.3 src/store/index.tsx">6.3 src/store/index.tsx</a></li><li><a href="#t116.4 store/action-types.tsx">6.4 store/action-types.tsx</a></li><li><a href="#t126.5 reducers/index.tsx">6.5 reducers/index.tsx</a></li><li><a href="#t136.6 actions/index.tsx">6.6 actions/index.tsx</a></li><li><a href="#t146.7 types/index.tsx">6.7 types/index.tsx</a></li></ul></li><li><a href="#t157. 合并reducers">7. 合并reducers</a><ul><li><a href="#t167.1 src/index.tsx">7.1 src/index.tsx</a></li><li><a href="#t177.2 /action-types.tsx">7.2 /action-types.tsx</a></li><li><a href="#t187.3 reducers/index.tsx">7.3 reducers/index.tsx</a></li><li><a href="#t197.4 types/index.tsx">7.4 types/index.tsx</a></li><li><a href="#t207.5 components/Counter1.tsx">7.5 components/Counter1.tsx</a></li><li><a href="#t217.6 src/components/Counter2.tsx">7.6 src/components/Counter2.tsx</a></li><li><a href="#t227.7 actions/counter1.tsx">7.7 actions/counter1.tsx</a></li><li><a href="#t237.8 actions/counter2.tsx">7.8 actions/counter2.tsx</a></li><li><a href="#t247.9 reducers/counter1.tsx">7.9 reducers/counter1.tsx</a></li><li><a href="#t257.10 reducers/counter2.tsx">7.10 reducers/counter2.tsx</a></li></ul></li><li><a href="#t268.配置路由">8.配置路由</a><ul><li><a href="#t278.1  src/index.tsx">8.1  src/index.tsx</a></li></ul></li><li><a href="#t289. connected-react-router">9. connected-react-router</a><ul><li><a href="#t299.1 components/Counter1.tsx">9.1 components/Counter1.tsx</a></li><li><a href="#t309.3 actions/counter1.tsx">9.3 actions/counter1.tsx</a></li><li><a href="#t319.4 src/store/index.tsx">9.4 src/store/index.tsx</a></li><li><a href="#t329.5 reducers/index.tsx">9.5 reducers/index.tsx</a></li><li><a href="#t339.6 store/history.tsx">9.6 store/history.tsx</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;">1. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE; <a href="#t01. &#x521D;&#x59CB;&#x5316;&#x9879;&#x76EE;"> # </a></h2>
<pre><code class="lang-js">mkdir react-typesript
cd react-typesript
cnpm init -y
touch .gitignore
</code></pre>
<h2 id="t12.&#x5B89;&#x88C5;&#x4F9D;&#x8D56;">2.&#x5B89;&#x88C5;&#x4F9D;&#x8D56; <a href="#t12.&#x5B89;&#x88C5;&#x4F9D;&#x8D56;"> # </a></h2>
<ul>
<li>@types&#x5F00;&#x5934;&#x7684;&#x5305;&#x90FD;&#x662F;typeScript&#x7684;&#x58F0;&#x660E;&#x6587;&#x4EF6;&#xFF0C;&#x53EF;&#x4EE5;&#x8FDB;&#x5165;node_modules/@types/XX/index.d.ts&#x8FDB;&#x884C;&#x67E5;&#x770B;</li>
<li><a href="https://github.com/DefinitelyTyped/DefinitelyTyped">&#x58F0;&#x660E;&#x6587;&#x4EF6;</a></li>
</ul>
<pre><code class="lang-js">cnpm i react react-dom @types/react @types/react-dom react-router-dom @types/react-router-dom -S
cnpm i webpack webpack-cli webpack-dev-server html-webpack-plugin -D
cnpm i typescript ts-loader source-map-loader -D
cnpm i redux react-redux @types/react-redux redux-thunk  redux-logger -S
cnpm i connected-react-router -S
</code></pre>
<ul>
<li>ts-loader&#x53EF;&#x4EE5;&#x8BA9;Webpack&#x4F7F;&#x7528;TypeScript&#x7684;&#x6807;&#x51C6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;tsconfig.json&#x7F16;&#x8BD1;TypeScript&#x4EE3;&#x7801;&#x3002;</li>
<li>source-map-loader&#x4F7F;&#x7528;&#x4EFB;&#x610F;&#x6765;&#x81EA;Typescript&#x7684;sourcemap&#x8F93;&#x51FA;&#xFF0C;&#x4EE5;&#x6B64;&#x901A;&#x77E5;webpack&#x4F55;&#x65F6;&#x751F;&#x6210;&#x81EA;&#x5DF1;&#x7684;sourcemaps,&#x8FD9;&#x8BA9;&#x4F60;&#x5728;&#x8C03;&#x8BD5;&#x6700;&#x7EC8;&#x751F;&#x6210;&#x7684;&#x6587;&#x4EF6;&#x65F6;&#x5C31;&#x597D;&#x50CF;&#x5728;&#x8C03;&#x8BD5;TypeScript&#x6E90;&#x7801;&#x4E00;&#x6837;&#x3002;</li>
</ul>
<h2 id="t23.&#x652F;&#x6301;typescript">3.&#x652F;&#x6301;typescript <a href="#t23.&#x652F;&#x6301;typescript"> # </a></h2>
<p>&#x9700;&#x8981;&#x751F;&#x6210;&#x4E00;&#x4E2A;tsconfig.json&#x6587;&#x4EF6;&#x6765;&#x544A;&#x8BC9;ts-loader&#x5982;&#x4F55;&#x7F16;&#x8BD1;&#x4EE3;&#x7801;TypeScript&#x4EE3;&#x7801;</p>
<pre><code class="lang-js">tsc --init
</code></pre>
<pre><code class="lang-json">{
  <span class="hljs-attr">&quot;compilerOptions&quot;</span>: {
    <span class="hljs-attr">&quot;outDir&quot;</span>: <span class="hljs-string">&quot;./dist&quot;</span>,
    <span class="hljs-attr">&quot;sourceMap&quot;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">&quot;noImplicitAny&quot;</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">&quot;module&quot;</span>: <span class="hljs-string">&quot;commonjs&quot;</span>,
    <span class="hljs-attr">&quot;target&quot;</span>: <span class="hljs-string">&quot;es5&quot;</span>,
    <span class="hljs-attr">&quot;jsx&quot;</span>: <span class="hljs-string">&quot;react&quot;</span>
  },
  <span class="hljs-attr">&quot;include&quot;</span>: [
    <span class="hljs-string">&quot;./src/**/*&quot;</span>
  ]
}
</code></pre>
<ul>
<li>outDir &#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x76EE;&#x5F55;</li>
<li>sourceMap&#xFF1A;&#x628A; ts &#x6587;&#x4EF6;&#x7F16;&#x8BD1;&#x6210; js &#x6587;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x540C;&#x65F6;&#x751F;&#x6210;&#x5BF9;&#x5E94;&#x7684;sourceMap&#x6587;&#x4EF6;</li>
<li>noImplicitAny&#xFF1A;&#x5982;&#x679C;&#x4E3A;true&#x7684;&#x8BDD;&#xFF0C;TypeScript &#x7F16;&#x8BD1;&#x5668;&#x65E0;&#x6CD5;&#x63A8;&#x65AD;&#x51FA;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5B83;&#x4ECD;&#x7136;&#x4F1A;&#x751F;&#x6210; JavaScript &#x6587;&#x4EF6;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4E5F;&#x4F1A;&#x62A5;&#x544A;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;</li>
<li>module&#xFF1A;&#x4EE3;&#x7801;&#x89C4;&#x8303;</li>
<li>target&#xFF1A;&#x8F6C;&#x6362;&#x6210;es5</li>
<li>jsx&#xFF1A;react&#x6A21;&#x5F0F;&#x4F1A;&#x751F;&#x6210;React.createElement&#xFF0C;&#x5728;&#x4F7F;&#x7528;&#x524D;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x4E86;&#xFF0C;&#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x7684;&#x6269;&#x5C55;&#x540D;&#x4E3A;.js</li>
<li>include&#xFF1A;&#x9700;&#x8981;&#x7F16;&#x8BD1;&#x7684;&#x76EE;&#x5F55;&#x3002;</li>
</ul>
<h2 id="t34.&#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6;">4.&#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6; <a href="#t34.&#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6;"> # </a></h2>
<p>webpack.config.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> webpack=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack&apos;</span>);
<span class="hljs-keyword">const</span> HtmlWebpackPlugin=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;html-webpack-plugin&apos;</span>);
<span class="hljs-keyword">const</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-built_in">module</span>.exports={
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&quot;./src/index.tsx&quot;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">filename</span>: <span class="hljs-string">&quot;bundle.js&quot;</span>,
        <span class="hljs-attr">path</span>: path.join(__dirname,<span class="hljs-string">&apos;dist&apos;</span>)
    },
    <span class="hljs-attr">devtool</span>: <span class="hljs-string">&quot;source-map&quot;</span>,
    <span class="hljs-attr">devServer</span>: {
        <span class="hljs-attr">hot</span>: <span class="hljs-literal">true</span>,
        <span class="hljs-attr">contentBase</span>: path.join(__dirname,<span class="hljs-string">&apos;dist&apos;</span>),
        <span class="hljs-attr">historyApiFallback</span>: {
            <span class="hljs-attr">index</span>:<span class="hljs-string">&apos;./index.html&apos;</span>
        }
    },
    <span class="hljs-attr">resolve</span>: {
        <span class="hljs-attr">extensions</span>: [<span class="hljs-string">&quot;.ts&quot;</span>, <span class="hljs-string">&quot;.tsx&quot;</span>, <span class="hljs-string">&quot;.js&quot;</span>, <span class="hljs-string">&quot;.json&quot;</span>]
    },

    <span class="hljs-attr">module</span>: {
        <span class="hljs-attr">rules</span>: [{
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.tsx?$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&quot;ts-loader&quot;</span>
            },

            {
                <span class="hljs-attr">enforce</span>: <span class="hljs-string">&quot;pre&quot;</span>,
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">loader</span>: <span class="hljs-string">&quot;source-map-loader&quot;</span>
            }
        ]
    },

    <span class="hljs-attr">plugins</span>: [
        <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
            <span class="hljs-attr">template</span>:<span class="hljs-string">&apos;./src/index.html&apos;</span>
        }),
        <span class="hljs-keyword">new</span> webpack.HotModuleReplacementPlugin()
    ],
};
</code></pre>
<h2 id="t45.&#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6;">5.&#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6; <a href="#t45.&#x8BA1;&#x6570;&#x5668;&#x7EC4;&#x4EF6;"> # </a></h2>
<h3 id="t55.1 src/index.tsx">5.1 src/index.tsx <a href="#t55.1 src/index.tsx"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter&apos;</span>;
ReactDOM.render((
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Counter</span> <span class="hljs-attr">number</span>=<span class="hljs-string">{100}/</span>&gt;</span>
),document.getElementById(&apos;root&apos;));
</span></code></pre>
<h3 id="t65.2 components/Counter.tsx">5.2 components/Counter.tsx <a href="#t65.2 components/Counter.tsx"> # </a></h3>
<p>src/components/Counter.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> interface Props{
    <span class="hljs-attr">number</span>: number
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span>&lt;<span class="hljs-title">Props</span>&gt;</span>{
    render() {
        <span class="hljs-keyword">const</span> {number}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t76. &#x4F7F;&#x7528;redux">6. &#x4F7F;&#x7528;redux <a href="#t76. &#x4F7F;&#x7528;redux"> # </a></h2>
<h3 id="t86.1 src/index.tsx">6.1 src/index.tsx <a href="#t86.1 src/index.tsx"> # </a></h3>
<p>src/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> Counter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter&apos;</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./store&apos;</span>;
ReactDOM.render((
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Counter</span>/&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
),<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;root&apos;</span>));
</code></pre>
<h3 id="t96.2 components/Counter.tsx">6.2 components/Counter.tsx <a href="#t96.2 components/Counter.tsx"> # </a></h3>
<p>src/components/Counter.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> { Store } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/types&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions&apos;</span>;
<span class="hljs-keyword">export</span> interface Props{
    <span class="hljs-attr">number</span>: number,
    <span class="hljs-attr">increment</span>: any,
    <span class="hljs-attr">decrement</span>: any
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span>&lt;<span class="hljs-title">Props</span>&gt;</span>{
    render() {
        <span class="hljs-keyword">const</span> {number,increment,decrement}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{increment}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{decrement}</span>&gt;</span>-<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}

<span class="hljs-keyword">let</span> mapStateToProps=<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state:Store</span>):<span class="hljs-title">Store</span> </span>{
    <span class="hljs-keyword">return</span> state;
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(mapStateToProps,actions)(Counter);
</code></pre>
<h3 id="t106.3 src/store/index.tsx">6.3 src/store/index.tsx <a href="#t106.3 src/store/index.tsx"> # </a></h3>
<p>src/store/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {createStore } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>
<span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">let</span> store=createStore(reducers);
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<h3 id="t116.4 store/action-types.tsx">6.4 store/action-types.tsx <a href="#t116.4 store/action-types.tsx"> # </a></h3>
<p>src/store/action-types.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> DECREMENT=<span class="hljs-string">&apos;DECREMENT&apos;</span>;
</code></pre>
<h3 id="t126.5 reducers/index.tsx">6.5 reducers/index.tsx <a href="#t126.5 reducers/index.tsx"> # </a></h3>
<p>src/store/reducers/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> { Store } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../types&apos;</span>;
<span class="hljs-keyword">import</span> {Action} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state: Store={ number: <span class="hljs-number">0</span> },action: Action</span>): <span class="hljs-title">Store</span> </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.INCREMENT:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number+<span class="hljs-number">1</span>};
        <span class="hljs-keyword">case</span> types.DECREMENT:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number<span class="hljs-number">-1</span>};
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t136.6 actions/index.tsx">6.6 actions/index.tsx <a href="#t136.6 actions/index.tsx"> # </a></h3>
<p>src/store/actions/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {INCREMENT,DECREMENT} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> interface Increment{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> INCREMENT
}
<span class="hljs-keyword">export</span> interface Decrement{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> DECREMENT
}
<span class="hljs-keyword">export</span> type Action=Increment|Decrement;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">increment</span>(<span class="hljs-params"></span>): <span class="hljs-title">Increment</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: INCREMENT };
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decrement</span>(<span class="hljs-params"></span>):<span class="hljs-title">Decrement</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: DECREMENT };
}
</code></pre>
<h3 id="t146.7 types/index.tsx">6.7 types/index.tsx <a href="#t146.7 types/index.tsx"> # </a></h3>
<p>src/store/types/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> interface Store{
    <span class="hljs-attr">number</span>: number
}
</code></pre>
<h2 id="t157. &#x5408;&#x5E76;reducers">7. &#x5408;&#x5E76;reducers <a href="#t157. &#x5408;&#x5E76;reducers"> # </a></h2>
<h3 id="t167.1 src/index.tsx">7.1 src/index.tsx <a href="#t167.1 src/index.tsx"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> Counter1 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter1&apos;</span>;
<span class="hljs-keyword">import</span> Counter2 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter2&apos;</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./store&apos;</span>;
ReactDOM.render((
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">React.Fragment</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Counter1</span>/&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Counter2</span>/&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">React.Fragment</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span>
),document.getElementById(&apos;root&apos;));
</span></code></pre>
<h3 id="t177.2 /action-types.tsx">7.2 /action-types.tsx <a href="#t177.2 /action-types.tsx"> # </a></h3>
<p>src/store/action-types.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT=<span class="hljs-string">&apos;INCREMENT&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> DECREMENT=<span class="hljs-string">&apos;DECREMENT&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT1=<span class="hljs-string">&apos;INCREMENT1&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> DECREMENT1=<span class="hljs-string">&apos;DECREMENT1&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> INCREMENT2=<span class="hljs-string">&apos;INCREMENT2&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> DECREMENT2=<span class="hljs-string">&apos;DECREMENT2&apos;</span>;
</code></pre>
<h3 id="t187.3 reducers/index.tsx">7.3 reducers/index.tsx <a href="#t187.3 reducers/index.tsx"> # </a></h3>
<p>src/store/reducers/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> counter1 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter1&apos;</span>;
<span class="hljs-keyword">import</span> counter2 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter2&apos;</span>;
<span class="hljs-keyword">import</span> { combineReducers } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">let</span> reducers=combineReducers({
    counter1,
    counter2
});
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducers;
</code></pre>
<h3 id="t197.4 types/index.tsx">7.4 types/index.tsx <a href="#t197.4 types/index.tsx"> # </a></h3>
<p>src/store/types/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> interface Store{
    <span class="hljs-attr">counter1</span>: Counter1,
    <span class="hljs-attr">counter2</span>: Counter2
}
<span class="hljs-keyword">export</span> interface Counter1{
    <span class="hljs-attr">number</span>: number
}
<span class="hljs-keyword">export</span> interface Counter2{
    <span class="hljs-attr">number</span>: number
}
</code></pre>
<h3 id="t207.5 components/Counter1.tsx">7.5 components/Counter1.tsx <a href="#t207.5 components/Counter1.tsx"> # </a></h3>
<p>src/components/Counter1.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/types&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/counter1&apos;</span>;
<span class="hljs-keyword">export</span> interface Props{
    <span class="hljs-attr">number</span>: number,
    <span class="hljs-attr">increment1</span>: any,
    <span class="hljs-attr">decrement1</span>: any
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter1</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span>&lt;<span class="hljs-title">Props</span>&gt;</span>{
    render() {
        <span class="hljs-keyword">const</span> {number,increment1,decrement1}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{increment1}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{decrement1}</span>&gt;</span>-<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}

<span class="hljs-keyword">let</span> mapStateToProps=<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state:types.Store</span>):<span class="hljs-title">types</span>.<span class="hljs-title">Counter1</span> </span>{
    <span class="hljs-keyword">return</span> state.counter1;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(mapStateToProps,actions)(Counter1);
</code></pre>
<h3 id="t217.6 src/components/Counter2.tsx">7.6 src/components/Counter2.tsx <a href="#t217.6 src/components/Counter2.tsx"> # </a></h3>
<p> src/components/Counter2.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/types&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> actions <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../store/actions/counter2&apos;</span>;
<span class="hljs-keyword">export</span> interface Props{
    <span class="hljs-attr">number</span>: number,
    <span class="hljs-attr">increment2</span>: any,
    <span class="hljs-attr">decrement2</span>: any
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter2</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span>&lt;<span class="hljs-title">Props</span>&gt;</span>{
    render() {
        <span class="hljs-keyword">const</span> {number,increment2,decrement2}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{increment2}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{decrement2}</span>&gt;</span>-<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}

<span class="hljs-keyword">let</span> mapStateToProps=<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state:types.Store</span>):<span class="hljs-title">types</span>.<span class="hljs-title">Counter2</span> </span>{
    <span class="hljs-keyword">return</span> state.counter2;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(mapStateToProps,actions)(Counter2);
</code></pre>
<h3 id="t227.7 actions/counter1.tsx">7.7 actions/counter1.tsx <a href="#t227.7 actions/counter1.tsx"> # </a></h3>
<p>src/store/actions/counter1.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {INCREMENT1,DECREMENT1} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> interface Increment1{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> INCREMENT1
}
<span class="hljs-keyword">export</span> interface Decrement1{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> DECREMENT1
}
<span class="hljs-keyword">export</span> type Action=Increment1|Decrement1;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">increment1</span>(<span class="hljs-params"></span>): <span class="hljs-title">Increment1</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: INCREMENT1 };
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decrement1</span>(<span class="hljs-params"></span>):<span class="hljs-title">Decrement1</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: DECREMENT1 };
}
</code></pre>
<h3 id="t237.8 actions/counter2.tsx">7.8 actions/counter2.tsx <a href="#t237.8 actions/counter2.tsx"> # </a></h3>
<p>src/store/actions/counter2.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {INCREMENT2,DECREMENT2} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> interface Increment2{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> INCREMENT2
}
<span class="hljs-keyword">export</span> interface Decrement2{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> DECREMENT2
}
<span class="hljs-keyword">export</span> type Action=Increment2|Decrement2;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">increment2</span>(<span class="hljs-params"></span>): <span class="hljs-title">Increment2</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: INCREMENT2 };
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decrement2</span>(<span class="hljs-params"></span>):<span class="hljs-title">Decrement2</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: DECREMENT2 };
}
</code></pre>
<h3 id="t247.9 reducers/counter1.tsx">7.9 reducers/counter1.tsx <a href="#t247.9 reducers/counter1.tsx"> # </a></h3>
<p>src/store/reducers/counter1.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> { Counter1 } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../types&apos;</span>;
<span class="hljs-keyword">import</span> {Action} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions/counter1&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state: Counter1={ number: <span class="hljs-number">0</span> },action: Action</span>): <span class="hljs-title">Counter1</span> </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.INCREMENT1:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number+<span class="hljs-number">1</span>};
        <span class="hljs-keyword">case</span> types.DECREMENT1:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number<span class="hljs-number">-1</span>};
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t257.10 reducers/counter2.tsx">7.10 reducers/counter2.tsx <a href="#t257.10 reducers/counter2.tsx"> # </a></h3>
<p>src/store/reducers/counter2.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> { Counter2 } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../types&apos;</span>;
<span class="hljs-keyword">import</span> {Action} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../actions/counter2&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state: Counter2={ number: <span class="hljs-number">0</span> },action: Action</span>): <span class="hljs-title">Counter2</span> </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.INCREMENT2:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number+<span class="hljs-number">1</span>};
        <span class="hljs-keyword">case</span> types.DECREMENT2:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">number</span>:state.number<span class="hljs-number">-1</span>};
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h2 id="t268.&#x914D;&#x7F6E;&#x8DEF;&#x7531;">8.&#x914D;&#x7F6E;&#x8DEF;&#x7531; <a href="#t268.&#x914D;&#x7F6E;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t278.1  src/index.tsx">8.1  src/index.tsx <a href="#t278.1  src/index.tsx"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> Counter1 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter1&apos;</span>;
<span class="hljs-keyword">import</span> Counter2 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Counter2&apos;</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./store&apos;</span>;
<span class="hljs-keyword">import</span> {BrowserRouter <span class="hljs-keyword">as</span> Router, Route,Link } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
ReactDOM.render((
    &lt;Provider store={store}&gt;
        &lt;Router &gt;
            &lt;React.Fragment&gt;
                &lt;Link to=&quot;/counter1&quot;&gt;counter1&lt;/Link&gt;
                &lt;Link to=&quot;/counter2&quot;&gt;counter2&lt;/Link&gt;
                &lt;Route path=&quot;/counter1&quot; component={Counter1} /&gt;
                  &lt;Route path=&quot;/counter2&quot; component={Counter2}/&gt;
         &lt;/React.Fragment&gt;
        &lt;/Router&gt;

    &lt;/Provider&gt;
),document.getElementById(&apos;root&apos;));
</code></pre>
<h2 id="t289. connected-react-router">9. connected-react-router <a href="#t289. connected-react-router"> # </a></h2>
<ul>
<li><a href="https://github.com/supasate/connected-react-router">connected-react-router</a><h3 id="t299.1 components/Counter1.tsx">9.1 components/Counter1.tsx <a href="#t299.1 components/Counter1.tsx"> # </a></h3>
src/components/Counter1.tsx
<code>`</code>js
import <em> as React from &apos;react&apos;;
import { connect } from &apos;react-redux&apos;;
import </em> as types from &apos;../store/types&apos;;
import * as actions from &apos;../store/actions/counter1&apos;;
export interface Props{
  number: number,
  increment1: any,
  decrement1: any,
  goCounter2: any
}
class Counter1 extends React.Component<props>{
  render() {<pre><code>  const {number,increment1,decrement1,goCounter2}=this.props;
  return (
      &lt;div&gt;
          &lt;p&gt;{number}&lt;/p&gt;
          &lt;button onClick={increment1}&gt;+&lt;/button&gt;
          &lt;button onClick={decrement1}&gt;-&lt;/button&gt;
          &lt;button onClick={goCounter2}&gt;goCounter2&lt;/button&gt;
      &lt;/div&gt;
  )
</code></pre>  }
}</props></li>
</ul>
<p>let mapStateToProps=function (state:types.Store):types.Counter1 {
    return state.counter1;
}</p>
<p>export default connect(mapStateToProps,actions)(Counter1);</p>
<pre><code>
### 9.2 src/index.tsx
src/index.tsx
```js
import * as React from &apos;react&apos;;
import * as ReactDOM from &apos;react-dom&apos;;
import Counter1 from &apos;./components/Counter1&apos;;
import Counter2 from &apos;./components/Counter2&apos;;
import { Provider } from &apos;react-redux&apos;;
import store from &apos;./store&apos;;
import {Route,Link } from &apos;react-router-dom&apos;;
import { ConnectedRouter } from &apos;connected-react-router&apos;
import history from &apos;./store/history&apos;;
ReactDOM.render((
    &lt;Provider store={store}&gt;
        &lt;ConnectedRouter history={history}&gt;
            &lt;React.Fragment&gt;
                &lt;Link to=&quot;/counter1&quot;&gt;counter1&lt;/Link&gt;
                &lt;Link to=&quot;/counter2&quot;&gt;counter2&lt;/Link&gt;
            &lt;Route path=&quot;/counter1&quot; component={Counter1} /&gt;
              &lt;Route path=&quot;/counter2&quot; component={Counter2}/&gt;
         &lt;/React.Fragment&gt;
        &lt;/ConnectedRouter&gt;
    &lt;/Provider&gt;
),document.getElementById(&apos;root&apos;));
</code></pre><h3 id="t309.3 actions/counter1.tsx">9.3 actions/counter1.tsx <a href="#t309.3 actions/counter1.tsx"> # </a></h3>
<p> src/store/actions/counter1.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {INCREMENT1,DECREMENT1} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">import</span> { push } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;connected-react-router&apos;</span>;

<span class="hljs-keyword">export</span> interface Increment1{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> INCREMENT1
}
<span class="hljs-keyword">export</span> interface Decrement1{
    <span class="hljs-attr">type</span>:<span class="hljs-keyword">typeof</span> DECREMENT1
}
<span class="hljs-keyword">export</span> type Action=Increment1|Decrement1;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">increment1</span>(<span class="hljs-params"></span>): <span class="hljs-title">any</span> </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">dispatch:any,getState:any</span>) </span>{
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            dispatch({
                <span class="hljs-attr">type</span>:INCREMENT1
            })
        },<span class="hljs-number">1000</span>);
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decrement1</span>(<span class="hljs-params"></span>):<span class="hljs-title">Decrement1</span> </span>{
    <span class="hljs-keyword">return</span> { <span class="hljs-attr">type</span>: DECREMENT1 };
}
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">goCounter2</span>(<span class="hljs-params"></span>):<span class="hljs-title">any</span> </span>{
    <span class="hljs-keyword">return</span> push(<span class="hljs-string">&apos;/counter2&apos;</span>);
}
</code></pre>
<h3 id="t319.4 src/store/index.tsx">9.4 src/store/index.tsx <a href="#t319.4 src/store/index.tsx"> # </a></h3>
<p>src/store/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {createStore,applyMiddleware} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>
<span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">import</span> { routerMiddleware } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;connected-react-router&apos;</span>
<span class="hljs-keyword">import</span> history <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./history&apos;</span>;
<span class="hljs-keyword">import</span> thunk <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-thunk&apos;</span>;
<span class="hljs-keyword">import</span> logger <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-logger&apos;</span>;
<span class="hljs-keyword">let</span> router = routerMiddleware(history);
<span class="hljs-keyword">let</span> store=createStore(reducers,applyMiddleware(router,thunk,logger));
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<h3 id="t329.5 reducers/index.tsx">9.5 reducers/index.tsx <a href="#t329.5 reducers/index.tsx"> # </a></h3>
<p>src/store/reducers/index.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> counter1 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter1&apos;</span>;
<span class="hljs-keyword">import</span> counter2 <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./counter2&apos;</span>;
<span class="hljs-keyword">import</span> { combineReducers } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> history <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../history&apos;</span>;
<span class="hljs-keyword">import</span> { connectRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;connected-react-router&apos;</span>
<span class="hljs-keyword">let</span> reducers=combineReducers({
    counter1,
    counter2,
    <span class="hljs-attr">router</span>: connectRouter(history)
});
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> reducers;
</code></pre>
<h3 id="t339.6 store/history.tsx">9.6 store/history.tsx <a href="#t339.6 store/history.tsx"> # </a></h3>
<p>src/store/history.tsx</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {createBrowserHistory} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;history&apos;</span>
<span class="hljs-keyword">const</span> history=createBrowserHistory()
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> history;
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
